package arkham.gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JLabel;

import arkham.util.ImageUtils;

/**
 * This class creates an image label of a fixed size. New images will be
 * resized to the existing size.
 * @author ctheng
 *
 */
public class ImageLabel extends JLabel
{
	private static final long serialVersionUID = 2882193216249708593L;
	private BufferedImage image;
	private int width, height;

	// Create an empty image with a specified size
	public ImageLabel(int width, int height)
	{
		super();
		this.width = width;
		this.height = height;
		Dimension size = new Dimension(width, height);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		setBorder(null);
		setLayout(null);
		setVisible(true);
	}

	public ImageLabel(BufferedImage img)
	{
		super();
		this.image = img;
		this.width = img.getWidth(null);
		this.height = img.getHeight(null);
		Dimension size = new Dimension(width, height);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		setSize(size);
		setBorder(null);
		setLayout(null);
		setVisible(true);
	}

	public ImageLabel(BufferedImage img, String altText)
	{
		this(img);
		setToolTipText(altText);
	}

	public void setImage(BufferedImage img)
	{
		this.image = ImageUtils.resizeImage(img, width, height);
	}

	public void paintComponent(Graphics g)
	{
		if (image != null)
		{
			g.drawImage(image, 0, 0, null);
		}
	}
}
